package cn.jetpiece.cloud.log.app.config;

import cn.jetpiece.cloud.log.app.core.handler.LogMateObjectHandler;
import cn.jetpiece.cloud.core.handler.DynamicTableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan(basePackages = {"cn.jetpiece.cloud.**.mapper"})
public class LogDataSourceConfig {


    /**
     * 自定义公共字段自动注入
     */
    @Bean
    public LogMateObjectHandler metaObjectHandler() {
        return new LogMateObjectHandler();
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        /*
         * 动态表名插件
         */
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler(new DynamicTableNameHandler());
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        /*
         * mybatis-plus分页插件
         */
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        /*
         * 乐观锁mybatis插件
         */
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}
